Plotly website: “plotly is an R package for creating interactive web-based graphs via the open source JavaScript graphing library plotly.js.”
We are going to explore plotly as a ggplot2
extension that will allow as to create interactive plots from ggplot2
objects.
Plotly is one the most popular library to create interactive plots for its extensive maintenance, documentation, and most important, it looks beautiful. However, the top 1 reason is how easy to use is with the adaptation of ggplot2.
# From CRAN
install.packages("plotly")
# From github
devtools::install_github("ropensci/plotly")
This function converts a ggplot2::ggplot() object to a
plotly object.
ggplotly(
p = ggplot2::last_plot(),
width = NULL,
height = NULL,
tooltip = "all",
dynamicTicks = FALSE,
layerData = 1,
originalData = TRUE,
source = "A",
...
)
Now, we are going to explore multiple examples of the integration
between ggplot2 and plotly with the parsing function ggplotly()
# Tip: Always load libraries at the beginning of the markdown documetns
library(tidyverse)
library(plotly)
library(ggsci)
ggpenguins <- palmerpenguins::penguins %>%
ggplot(aes( bill_length_mm , body_mass_g, color = species )) +
geom_point() +
scale_color_d3() +
theme_bw()
ggpenguins
Things you have can do with an interactive plot
ggplotly(ggpenguins)
# Library here just to make emphasis
library(ggfortify)
In this case we are going to explore a new library
ggfortify that accepts prcomp results and
creates an automatic scores PCA plot.
After you perform PCA analysis, you can use autorplot()
to create a ggplot2 object for using in the ggplotly()
function.
df <- iris[1:4] # Extractict numeric variables
pca_res <- prcomp(df, scale. = TRUE) # Making PCA
p <- autoplot(pca_res, data = iris, colour = 'Species') + # PCA autoplot
scale_color_d3()
ggplotly(p)
midwest: census data
percollege: Percent college educated
boxplot <- ggplot(midwest, aes(state, percollege, color = state) ) +
geom_boxplot() +
scale_color_d3() +
theme_bw() +
coord_flip()
ggplotly(boxplot)
density <- ggplot(diamonds, aes(x = price)) +
geom_density(aes(fill = "epanechnikov"), kernel = "epanechnikov") +
facet_grid(~cut) +
ggtitle("Kernel density estimate with Facets")
ggplotly(density)
stack_barplot <- ggplot(mpg, aes(class)) +
geom_bar(aes(fill = drv)) + scale_fill_d3() + theme_bw()
ggplotly(stack_barplot)
Toltip is an argument that controls the text that is shown when you hover the mouse over the data. By default, all mapping variables are shown. You can modify the order and the variables that are shown in the tooltip.
In the penguins data we have more variables that may want to include in the text shown in our plotly plot such as sex and island.
# dt[seq(10),] subset the ten first row and then use glimpse to shorten the output
glimpse(palmerpenguins::penguins[seq(10), ])
## Rows: 10
## Columns: 8
## $ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
## $ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
## $ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
## $ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
## $ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190
## $ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
## $ sex <fct> male, female, female, NA, female, male, female, male…
## $ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…
ggplotly(ggpenguins)
“colour” is requiered and “color” is not supported
ggplotly(ggpenguins,
tooltip = c("colour") )
You might not like the default hover text aesthetics, and can change them! You can do this using style and layout and adding these functions using the pipe %>%.
Code taken from BioDash
# setting fonts for the plot
font <- list(
family = "Courier New",
size = 15,
color = "white")
# setting hover label specs
label <- list(
bgcolor = "#3d1b40",
bordercolor = "transparent",
font = font) # we can do this bc we already set font
# amending our ggplotly call to include new fonts and hover label specs
ggplotly(ggpenguins, tooltip = "colour") %>%
style(hoverlabel = label) %>%
layout(font = font)
After you have done an amazing job creating a beautiful ggplot and made it interactive, you might want to save in a file. Here, you have two options, create a markdown and knit your interactive plot in a html file, if you knit in a static file such as pdf or word file, you will lose the interactive part of your plot.
You will need to assign the interactive plot to an object, and then, export or save your plot to an html file.
# assign ggplotly plot to an object
ggplotly_to_save <- ggplotly(ggpenguins, tooltip = "colour") %>%
style(hoverlabel = label) %>%
layout(font = font)
# save
saveWidget(widget = ggplotly_to_save,
file = "ggplotlying.html")